package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.wineshop.UserWineShopData;
import com.yanqu.road.entity.wingroom.message.WingRoomMessageDetailInfo;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wineshop.WineShopModule;
import com.yanqu.road.server.manger.activity.bulletscreen.ActivityBulletScreenMgr;
import com.yanqu.road.server.manger.activity.shushan.ShuShanMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMessageMgr;

public class AgentRename {

    /**
     * 管理查询酒肆名称
     * @param param
     * @return
     */
    public String adminQueryWineShop(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            WineShopModule wineShopModule = player.getModule(WineShopModule.class);
            UserWineShopData userWineShopData = wineShopModule.getUserWineShopData();
            if(null != userWineShopData){
                resultObj.put("data", userWineShopData.getWineShopName());
            }else {
                resultObj.put("data", "");
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    /**
     * 管理改名酒肆
     * @param param
     * @return
     */
    public String adminRenameWineShop(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        String changeContent = param.getString("changeContent");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            WineShopModule wineShopModule = player.getModule(WineShopModule.class);
            UserWineShopData userWineShopData = wineShopModule.getUserWineShopData();
            if(null != userWineShopData){
                userWineShopData.setWineShopName(changeContent);
                //改完名同步一下知名榜
                wineShopModule.changePopularityValueRank();
            }else {
                ret = eAgentErrorCode.ArgumentError.getValue();
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminQueryWingRoom(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            WingRoomMessageDetailInfo messageAllData = WingRoomMessageMgr.getSelfMessageAllData(player.getUserId(), false);
            if(null != messageAllData){
                resultObj.put("data", messageAllData.getSelfMessageContent());
            }else {
                resultObj.put("data", "");
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminRenameWingRoom(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        String changeContent = param.getString("changeContent");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            WingRoomProto.ChangeSelfMessageContentRespMsg.Builder resultResp = WingRoomMessageMgr.changeSelfMessageContent(player, changeContent);
            if(resultResp.getRet() != 0){
                ret = eAgentErrorCode.ArgumentError.getValue();
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminQueryBulletScreen(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        int activityId = param.getInteger("activityId");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            resultObj.put("data", ActivityBulletScreenMgr.queryPlayerBulletScreen(activityId, userId));
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminRenameBulletScreen(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        String changeContent = param.getString("changeContent");
        int activityId = param.getInteger("activityId");
        long contentId = param.getLongValue("contentId");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            if(0 != ActivityBulletScreenMgr.changePlayerBulletScreen(activityId, userId, changeContent, contentId)){
                ret = eAgentErrorCode.ArgumentError.getValue();
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminQueryShuShan(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        int activityId = param.getInteger("activityId");
        int ret = eAgentErrorCode.Success.getValue();
        resultObj.put("data", ShuShanMgr.adminQueryName(userId));
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }

    public String adminRenameShuShan(JSONObject param){
        JSONObject resultObj = AgentInterfaceMgr.getDefaultRet();
        long userId = param.getLongValue("userId");
        String changeContent = param.getString("changeContent");
        int activityId = param.getInteger("activityId");
        int ret = eAgentErrorCode.Success.getValue();
        GamePlayer player = GamePlayerMgr.getPlayer(userId);
        if(null == player){
            ret = eAgentErrorCode.ErrorPlayer.getValue();
        }else {
            if(0 != ShuShanMgr.canEntryActivity(player, activityId)){
                ret = eAgentErrorCode.ArgumentError.getValue();
            }else {
                if (0 != ShuShanMgr.adminChangeName(player, changeContent)) {
                    ret = eAgentErrorCode.ArgumentError.getValue();
                }
            }
        }
        AgentInterfaceMgr.setCommonRet(resultObj, ret);
        return resultObj.toString();
    }
}
